# HSI - Panethnic Identity ---------------------------------------------
library(TAM)

A <- designMatrices(resp=hsi_panethnic)$A
A1 <- A
dimnames(A1)[[1]] <- colnames(hsi_panethnic)
A1[1,,] = A1[4,,]
A1[2,,] = A1[5,,]
A1[3,,] = A1[6,,]

hsi_panethnic_m <- tam.mml(hsi_panethnic,
                                     group = hsi_language_group,
                                     irtmodel = "PCM",
                                     beta.fixed = cbind(c(1:3), 
                                     1, c(0, 0, 0)),
                                     A = A1)

hsi_panethnic_m <- tam.mml(hsi_panethnic,
                                   group = hsi_language_group,
                                   irtmodel = "PCM",
                                   beta.fixed = cbind(c(1:3), 
                                   1, c(0, 0, 0)),
                                   A = A1,
                                   xsi.fixed = cbind(c(1:8), 
                                                     hsi_panethnic_m$xsi.fixed.estimated
                                 [9:16,2]))

hsi_panethnic_m2 <- tam.mml(hsi_panethnic,
                                     group = hsi_language_group,
                                     irtmodel = "PCM",
                                     beta.fixed = cbind( c(1:3), 
                                                   1, c(0, 0, 0)))

hsi_panethnic_lr <- IRT.compareModels(hsi_panethnic_m,
                           hsi_panethnic_m2)

# HSI - American Identity -----------------------------------------------
A <- designMatrices(resp=hsi_american)$A
A1 <- A
dimnames(A1)[[1]] <- colnames(hsi_american)
A1[1,,] = A1[5,,]
A1[2,,] = A1[6,,]
A1[3,,] = A1[7,,]
A1[4,,] = A1[8,,]

hsi_american_m <- tam.mml(hsi_american,
                                     group = hsi_language_group,
                                     irtmodel = "PCM",
                                     beta.fixed = cbind(c(1:3), 
                                                        1, c(0, 0, 0)),
                                     A = A1)

hsi_american_m <- tam.mml(hsi_american,
                                     group = hsi_language_group,
                                     irtmodel = "PCM",
                                     beta.fixed = cbind(c(1:3), 
                                                        1, c(0, 0, 0)),
                                     A = A1,
                                     xsi.fixed = cbind(c(1:8), 
                                                       hsi_american_m$xsi.fixed.estimated[9:16,2]))

hsi_american_m2 <- tam.mml(hsi_american,
                                      group = hsi_language_group,
                                      irtmodel = "PCM",
                                      beta.fixed = cbind( c(1:3), 
                                                          1, c(0, 0, 0)))

hsi_american_lr <- IRT.compareModels(hsi_american_m,
                                 hsi_american_m2)

# HSI - Partisan Identity -----------------------------------------------
A <- designMatrices(resp=hsi_party)$A
A1 <- A
dimnames(A1)[[1]] <- colnames(hsi_party)
A1[1,,] = A1[4,,]
A1[2,,] = A1[5,,]
A1[3,,] = A1[6,,]

hsi_party_m <- tam.mml(hsi_party,
                          group = hsi_language_group,
                          irtmodel = "PCM",
                          beta.fixed = cbind(c(1:3), 
                                             1, c(0, 0, 0)),
                          A = A1)

hsi_party_m <- tam.mml(hsi_party,
                          group = hsi_language_group,
                          irtmodel = "PCM",
                          beta.fixed = cbind(c(1:3), 
                                             1, c(0, 0, 0)),
                          A = A1,
                          xsi.fixed = cbind(c(1:10), 
                                            hsi_party_m$xsi.fixed.estimated[11:20,2]))

hsi_party_m2 <- tam.mml(hsi_party,
                           group = hsi_language_group,
                           irtmodel = "PCM",
                           beta.fixed = cbind( c(1:3), 
                                               1, c(0, 0, 0)))

hsi_party_lr <- IRT.compareModels(hsi_party_m,
                                  hsi_party_m2)

# HSI - Immigration Opinion -----------------------------------------------
A <- designMatrices(resp=hsi_immigration)$A
A1 <- A
dimnames(A1)[[1]] <- colnames(hsi_immigration)
A1[1,,] = A1[5,,]
A1[2,,] = A1[6,,]
A1[3,,] = A1[7,,]
A1[4,,] = A1[8,,]

hsi_immigration_m <- tam.mml(hsi_immigration,
                     group = hsi_language_group,
                     irtmodel = "NRM",
                     beta.fixed = cbind(c(1:3), 
                                        1, c(0, 0, 0)),
                     A = A1)

hsi_immigration_m <- tam.mml(hsi_immigration,
                     group = hsi_language_group,
                     irtmodel = "NRM",
                     beta.fixed = cbind(c(1:3), 
                                        1, c(0, 0, 0)),
                     A = A1,
                     xsi.fixed = cbind(c(1:12), 
                                       hsi_immigration_m$xsi.fixed.estimated[13:24,2]))

hsi_immigration_m2 <- tam.mml(hsi_immigration,
                      group = hsi_language_group,
                      irtmodel = "NRM",
                      beta.fixed = cbind( c(1:3), 
                                          1, c(0, 0, 0)),
                      control = list(increment.factor = 1.05))

hsi_immigration_lr <- IRT.compareModels(hsi_immigration_m,
                            hsi_immigration_m2)
